package com.shj.service;


import com.shj.mapper.SysRoleMenuDao;
import com.shj.model.SysRoleMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 角色权限
 * @Author songhuajian
 * @Date 2020/1/12 23:14
 **/
@Service("sysRolePowerService")
public class SysRolePowerService {

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    /**
     * @description 根据权限id查询角色
     * @author songhuajian
     * @date 2020/1/12 23:17
     */
    public List<SysRoleMenu> findByMenuId(String menuId){
        return sysRoleMenuDao.queryByMenuId(menuId);
    }

    /**
     * @Description 根据角色id查找权限
     * @author songhuajian
     * @date 2020/4/6 15:02
     */
    public List<SysRoleMenu> findByRoleId(String roleId) { return sysRoleMenuDao.queryByRoleId(roleId);}

    /**
     * @Description 添加角色权限
     * @author songhuajian
     * @date 2020/4/6 15:25
     */
    @Transactional(rollbackFor = Exception.class)
    public void addRolePower(List<String> rolePower, String roleId) {
        for (String menuId : rolePower) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuDao.insert(sysRoleMenu);
        }
    }

    /**
     * @Description 更新角色权限
     * @author songhuajian
     * @date 2020/4/10 17:16
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRolePower(List<String> powers, String roleId) {
        // 删除数据关联
        sysRoleMenuDao.deleteById(roleId);
        // 添加新关联
        for (String menuId : powers) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuDao.insert(sysRoleMenu);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String roleId) {
        sysRoleMenuDao.deleteById(roleId);
    }
}
